import { logger } from '$common';
import { SimulationModelService } from '../service/modelService';
import { BaseResponese, ModelParseParamRequest, SimulationModel } from '../type';

/**
 * 仿真模型控制器
 */
export class SimulationModelController {
  /**
   * 获取仿真模型的目录树
   * @param modelFilePath 模型文件本地目录
   * @returns 
   */
  static async getModelFileTrees(modelFilePath: string): Promise<BaseResponese> {
    logger.debug(`🍺 SimulationModelController ==> getModelFileTrees, modelFilePath: ${modelFilePath}`);
    try {
      const simulationModel: SimulationModel = { file: modelFilePath, type: 'local' };
      const rootTreeNode = await new SimulationModelService(simulationModel).getModelDirectoryTrees();
      return { success: true, data: rootTreeNode.children };
    } catch (e) {
      return { success: false, message: e.message };
    }
  }

  /**
   * 解析仿真模型参数
   * @param request 
   * @returns 
   */
  static async parseModelParam(request: ModelParseParamRequest): Promise<BaseResponese> {
    logger.debug(`🍺 SimulationModelController ==> parseModelParam, modelFilePath: ${JSON.stringify(request, null, 2)}`);
    try {
      const simModel: SimulationModel = {
        type: request.type,
        file: request.modelPath,
        entry: request.entry
      };
      return await new SimulationModelService(simModel).parseModelParam(request.product);
    } catch (e) {
      logger.error(`❌ Failed to parse simulation params, error: ${e}`);
      return { success: false, message: e.message };
    }
  }
}